package com.investment.advisor.controller;

import com.investment.advisor.dto.FundDTO;
import com.investment.advisor.service.FundService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/funds")
@Api(tags = "基金管理", description = "基金的增删改查")
public class FundController {

    private final FundService fundService;

    @Autowired
    public FundController(FundService fundService) {
        this.fundService = fundService;
    }

    @GetMapping("/test")
    @ApiOperation("测试API是否正常工作")
    public ResponseEntity<String> testApi() {
        return ResponseEntity.ok("API工作正常");
    }

    @GetMapping
    @ApiOperation("获取所有基金")
    public ResponseEntity<List<FundDTO>> getAllFunds() {
        return ResponseEntity.ok(fundService.getAllFunds());
    }

    @GetMapping("/{id}")
    @ApiOperation("根据ID获取基金")
    public ResponseEntity<FundDTO> getFundById(
            @ApiParam(value = "基金ID", required = true)
            @PathVariable Long id) {
        return ResponseEntity.ok(fundService.getFundById(id));
    }

    @GetMapping("/code/{code}")
    @ApiOperation("根据基金代码获取基金")
    public ResponseEntity<FundDTO> getFundByCode(
            @ApiParam(value = "基金代码", required = true)
            @PathVariable String code) {
        return ResponseEntity.ok(fundService.getFundByCode(code));
    }

    @GetMapping("/search")
    @ApiOperation("搜索基金")
    public ResponseEntity<List<FundDTO>> searchFunds(
            @ApiParam(value = "搜索关键词", required = true)
            @RequestParam String keyword) {
        return ResponseEntity.ok(fundService.searchFunds(keyword));
    }

    @GetMapping("/type/{type}")
    @ApiOperation("根据基金类型获取基金")
    public ResponseEntity<List<FundDTO>> getFundsByType(
            @ApiParam(value = "基金类型", required = true)
            @PathVariable String type) {
        return ResponseEntity.ok(fundService.getFundsByType(type));
    }

    @GetMapping("/rating/{rating}")
    @ApiOperation("根据评级获取基金")
    public ResponseEntity<List<FundDTO>> getFundsByRating(
            @ApiParam(value = "最低评级", required = true)
            @PathVariable Integer rating) {
        return ResponseEntity.ok(fundService.getFundsByRating(rating));
    }

    @GetMapping("/type/{type}/rating/{rating}")
    @ApiOperation("根据类型和评级获取基金")
    public ResponseEntity<List<FundDTO>> getFundsByTypeAndRating(
            @ApiParam(value = "基金类型", required = true)
            @PathVariable String type,
            @ApiParam(value = "最低评级", required = true)
            @PathVariable Integer rating) {
        return ResponseEntity.ok(fundService.getFundsByTypeAndRating(type, rating));
    }

    @GetMapping("/tag/{tagName}")
    @ApiOperation("根据标签获取基金")
    public ResponseEntity<List<FundDTO>> getFundsByTagName(
            @ApiParam(value = "标签名称", required = true)
            @PathVariable String tagName) {
        return ResponseEntity.ok(fundService.getFundsByTagName(tagName));
    }

    @GetMapping("/top-performing")
    @ApiOperation("获取表现最好的基金")
    public ResponseEntity<List<FundDTO>> getTopPerformingFunds(
            @ApiParam(value = "返回的基金数量", defaultValue = "10")
            @RequestParam(defaultValue = "10") int limit) {
        return ResponseEntity.ok(fundService.getTopPerformingFunds(limit));
    }

    @PostMapping
    @ApiOperation("创建基金")
    public ResponseEntity<FundDTO> createFund(
            @ApiParam(value = "基金信息", required = true)
            @RequestBody FundDTO fundDTO) {
        return new ResponseEntity<>(fundService.createFund(fundDTO), HttpStatus.CREATED);
    }

    @PutMapping("/{id}")
    @ApiOperation("更新基金")
    public ResponseEntity<FundDTO> updateFund(
            @ApiParam(value = "基金ID", required = true)
            @PathVariable Long id,
            @ApiParam(value = "基金信息", required = true)
            @RequestBody FundDTO fundDTO) {
        return ResponseEntity.ok(fundService.updateFund(id, fundDTO));
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除基金")
    public ResponseEntity<Void> deleteFund(
            @ApiParam(value = "基金ID", required = true)
            @PathVariable Long id) {
        fundService.deleteFund(id);
        return ResponseEntity.noContent().build();
    }

    @PostMapping("/{fundId}/tags/{tagId}")
    @ApiOperation("为基金添加标签")
    public ResponseEntity<Void> addTagToFund(
            @ApiParam(value = "基金ID", required = true)
            @PathVariable Long fundId,
            @ApiParam(value = "标签ID", required = true)
            @PathVariable Long tagId) {
        fundService.addTagToFund(fundId, tagId);
        return ResponseEntity.noContent().build();
    }

    @DeleteMapping("/{fundId}/tags/{tagId}")
    @ApiOperation("从基金移除标签")
    public ResponseEntity<Void> removeTagFromFund(
            @ApiParam(value = "基金ID", required = true)
            @PathVariable Long fundId,
            @ApiParam(value = "标签ID", required = true)
            @PathVariable Long tagId) {
        fundService.removeTagFromFund(fundId, tagId);
        return ResponseEntity.noContent().build();
    }
} 